using System;
using System.Collections;
using UnityEngine;

public class Controller : MonoBehaviour
{
	private CharacterMovement movement;

	public CharacterInformation info;

	private CharacterStep step;

	private CharacterRotation rotation;

	private AimHelper aimHelper;

	private CharacterFighting fighting;

	private bool holdAttack;

	public GameJoystick joystick;

	private Coroutine worldFreezeCoro;

	private Game game;

	private bool canControl;

	public GameObject punchIcon;

	public GameObject shootIcon;

	public bool mobileController;

	public void ShowAttackIcon(bool hasWeapon)
	{
		punchIcon.SetActive(!hasWeapon);
		shootIcon.SetActive(hasWeapon);
	}

	public void HideController()
	{
		holdAttack = false;
		canControl = false;
	}

	public void ShowController()
	{
		canControl = true;
	}

	private void Update()
	{
		if (mobileController && joystick.Value.magnitude > 0.1f)
		{
			aimHelper.JoystickAim(joystick.Value);
		}
	}

	private void FixedUpdate()
	{
		if (!info.isAlive || info == null || !canControl)
		{
			return;
		}
		if (mobileController)
		{
			Vector2 value = joystick.Value;
			if (Mathf.Abs(value.x) > 0.1f)
			{
				step.PlayRunAnimation();
				CharacterMovement characterMovement = movement;
				Vector2 value2 = joystick.Value;
				characterMovement.Move(0f - value2.x);
			}
			else
			{
				step.StopRunAnimation();
			}
			Vector2 value3 = joystick.Value;
			if (value3.y < -0.95f)
			{
				movement.Sit();
			}
			if (holdAttack)
			{
				Attack();
			}
			return;
		}
		if (UnityEngine.Input.GetKeyDown(KeyCode.Space))
		{
			Jump();
		}
		if (UnityEngine.Input.GetKey(KeyCode.A))
		{
			step.PlayRunAnimation();
			movement.Move(1f);
		}
		else if (UnityEngine.Input.GetKey(KeyCode.D))
		{
			step.PlayRunAnimation();
			movement.Move(-1f);
		}
		else
		{
			step.StopRunAnimation();
		}
		float num = 1000f;
		CharacterInformation characterInformation = null;
		CharacterInformation[] players = game.players;
		foreach (CharacterInformation characterInformation2 in players)
		{
			if (characterInformation2 == info || !characterInformation2.isAlive)
			{
				continue;
			}
			Vector3 position = characterInformation2.mainRig.position;
			float z = position.z;
			Vector3 position2 = info.mainRig.position;
			float num2 = Mathf.Sign(z - position2.z);
			if ((num2 < 0f && rotation.lookingRight) || (num2 > 0f && !rotation.lookingRight))
			{
				float num3 = Vector3.Distance(info.mainRig.position, characterInformation2.mainRig.position);
				if (num3 < num)
				{
					characterInformation = characterInformation2;
					num = num3;
				}
			}
		}
		if (characterInformation != null)
		{
			aimHelper.aimTarget.transform.position = characterInformation.mainRig.position;
		}
		else
		{
			float num4 = (!rotation.lookingRight) ? 1f : (-1f);
			aimHelper.TargetAim(aimHelper.aimPosition.position + new Vector3(0f, 0f, num4 * 10f));
		}
		if (UnityEngine.Input.GetKey(KeyCode.K))
		{
			Attack();
		}
	}

	public void Jump()
	{
		if (info.isAlive && canControl)
		{
			if (info.sinceWall > info.sinceGrounded)
			{
				info.wallNormal = Vector3.zero;
			}
			if (info.isGrounded || info.sinceGrounded < 0.2f || info.wallNormal != Vector3.zero)
			{
				info.sinceGrounded = 1f;
				info.sinceWall = 1f;
				info.sinceJumped = 0f;
				info.jumpTick = 1;
				movement.Jump();
			}
			else if (info.jumpTick > 0)
			{
				info.sinceGrounded = 1f;
				info.sinceWall = 1f;
				info.sinceJumped = 0f;
				info.jumpTick--;
				movement.Jump();
			}
		}
	}

	public void OnHoldButtonAttack()
	{
		holdAttack = true;
	}

	public void OnReleaseButtonAttack()
	{
		holdAttack = false;
	}

	private void Attack()
	{
		if (info.isAlive && canControl)
		{
			fighting.Attack();
		}
	}

	public void SetControl(CharacterInformation info, Game game)
	{
		this.game = game;
		aimHelper = info.GetComponentInChildren<AimHelper>();
		fighting = info.GetComponent<CharacterFighting>();
		CharacterFighting characterFighting = fighting;
		characterFighting.OnWeaponChange = (Action<bool>)Delegate.Combine(characterFighting.OnWeaponChange, new Action<bool>(ShowAttackIcon));
		this.info = info;
		step = info.GetComponent<CharacterStep>();
		movement = info.GetComponent<CharacterMovement>();
		rotation = info.GetComponent<CharacterRotation>();
		ShowAttackIcon(hasWeapon: false);
	}

	public void UseHealthPowerUp()
	{
		if (info.isAlive && canControl && PersistenData.HealPowerUps > 0)
		{
			UsePowerUp(0);
		}
	}

	public void UseImmoPowerUp()
	{
		if (info.isAlive && canControl && PersistenData.ImmoPowerUps > 0)
		{
			UsePowerUp(1);
		}
	}

	public void UseFreezePowerUp()
	{
		if (info.isAlive && canControl && PersistenData.FreezePowerUps > 0)
		{
			UsePowerUp(2);
		}
	}

	public void UsePowerUp(int id)
	{
		switch (id)
		{
		case 0:
			PersistenData.HealPowerUps--;
			info.Heal();
			break;
		case 1:
			PersistenData.ImmoPowerUps--;
			info.Immortal(5f);
			break;
		case 2:
			PersistenData.FreezePowerUps--;
			if (worldFreezeCoro != null)
			{
				StopCoroutine(worldFreezeCoro);
			}
			worldFreezeCoro = StartCoroutine(coroWorldFreeze(3f, info));
			break;
		}
		game.ingameUI.ShowPowerUpCount();
	}

	private IEnumerator coroWorldFreeze(float duration, CharacterInformation caster)
	{
		CharacterInformation[] players = game.players;
		foreach (CharacterInformation characterInformation in players)
		{
			if (characterInformation != caster)
			{
				characterInformation.GetComponent<CharacterParticles>().PlayFreezeEffect(characterInformation.mainRig.position);
				characterInformation.Freeze(enableCollider: true);
			}
		}
		yield return new WaitForSeconds(duration);
		CharacterInformation[] players2 = game.players;
		foreach (CharacterInformation characterInformation2 in players2)
		{
			if (characterInformation2 != caster)
			{
				characterInformation2.DeFreeze();
			}
		}
	}
}
